package Controler.Cours;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Model.Listes;
import JDBC.Cours_Afficher;
import JDBC.Cours_Supprimer;

/**
 * Servlet implementation class SuppressionCours
 */
@WebServlet("/Cours_SupprimerController")
public class Cours_SupprimerController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Cours_SupprimerController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		String url = "ConfirmationSuppressionCours.jsp";
		
		if(request.getParameter("ok")!=null)
		{
			ArrayList<String> idCours = new ArrayList<String>();
			String all = request.getParameter("nbCours");
			int nbCours = Integer.parseInt(all);
			
			for (int i = 0; i < nbCours; i++) 
			{
				String checkbox = request.getParameter(String.valueOf(i));
				if(checkbox!=null)
				{
					idCours.add(checkbox);
				}
			}
			
			Cours_Supprimer suppression = new Cours_Supprimer();
			suppression.supprimer(idCours);
		}
		if(request.getParameter("out")!=null)
		{
			url = "GCours.jsp";
		}
		
		request.getRequestDispatcher(url).forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String url = "ListeCoursSuppression.jsp";
		
		if(request.getParameter("ok")!=null)
		{
			String idEleve = request.getParameter("eleve");
			String idProf = request.getParameter("professeur");
			
			Listes listes_cours = new Listes();
			Cours_Afficher cours = new Cours_Afficher();
			listes_cours.setMesCours(cours.afficher(Integer.parseInt(idEleve),Integer.parseInt(idProf)));
			
			request.setAttribute("mesCours",listes_cours);
		}
		if(request.getParameter("out")!=null)
		{
			url = "GCours.jsp";
		}
		request.getRequestDispatcher(url).forward(request, response);
	}

}
